package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.ShouhouFanxin;
import com.ruoyi.system.domain.ShouhouRecord;
import com.ruoyi.system.mapper.EpmShouhouReconditionMapper;
import com.ruoyi.system.service.IEpmShouhouRecoditionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class EpmShouhouRecoditionServiceImpl implements IEpmShouhouRecoditionService {

    @Autowired
    EpmShouhouReconditionMapper epmShouhouReconditionMapper;

    public List<ShouhouFanxin> list(ShouhouFanxin shouhouFanxin) {
        return epmShouhouReconditionMapper.list(shouhouFanxin);
    }

    @Transactional
    public void insertFanxinInfo(ShouhouFanxin shouhouFanxin) {
        shouhouFanxin.setMachineCondition(4);
        shouhouFanxin.setBackDate(new Date());
        shouhouFanxin.setOldSnId(shouhouFanxin.getSnId());
        epmShouhouReconditionMapper.insertFanxinInfo(shouhouFanxin);
        epmShouhouReconditionMapper.insertFanxin(shouhouFanxin);
    }


    public void update(ShouhouFanxin shouhouFanxin) {
        epmShouhouReconditionMapper.update(shouhouFanxin);
    }

    @Transactional
    public void shouhouFanxin(ShouhouFanxin shouhouFanxin) {
        shouhouFanxin.setFinishTime(new Date());
        epmShouhouReconditionMapper.shouhouFanxin(shouhouFanxin);
//        修改机器状态
        epmShouhouReconditionMapper.updateMachineCondition(shouhouFanxin.getOldSnId());
    }


    // 入库操作：
    // 1.修改机器状态
    // 2.复制翻新数据表数据到翻新仓库表中（新增数据）
    // 3.记录此次操作（新增数据）
    // 4.删除翻新数据表数据
    @Transactional
    public void fanxinRuku(ShouhouFanxin shouhouFanxin) {


/*        shouhouFanxin.setMachineCondition(6);
        shouhouFanxin.setReconditionTime(new Date());
        String snId = shouhouFanxin.getSnId();
        shouhouFanxin.setSnId(shouhouFanxin.getNewSnId());


        *//**
         * 这里插入仓库表语句可以暂时舍弃，待后续有相关要求再进行添加
         *//*
        epmShouhouReconditionMapper.insertFanxinRepository(shouhouFanxin);

        // 获取到所有信息保存在记录表中
        ShouhouRecord shouhouRecord = epmShouhouReconditionMapper.get(shouhouFanxin);
        shouhouRecord.setOperateType(2);
        shouhouRecord.setOperateTime(new Date());       // 最后操作时间（包括导出操作）
        shouhouRecord.setReconditionTime(new Date());   // 入库时间

        epmShouhouReconditionMapper.insertRecord(shouhouRecord);
        epmShouhouReconditionMapper.deleteFanxinInfo(snId);
        epmShouhouReconditionMapper.deleteFanxin(snId);
        */




        // 获取到所有信息保存在记录表中
        ShouhouRecord shouhouRecord = epmShouhouReconditionMapper.get(shouhouFanxin);
 

        shouhouRecord.setMachineCondition(6);
        shouhouRecord.setReconditionTime(new Date());
        String snId = shouhouRecord.getSnId();
        shouhouRecord.setSnId(shouhouFanxin.getNewSnId());


        shouhouRecord.setOperateType(2);
        shouhouRecord.setOperateTime(new Date());       // 最后操作时间（包括导出操作）
        shouhouRecord.setReconditionTime(new Date());   // 入库时间

        epmShouhouReconditionMapper.insertRecord(shouhouRecord);
        epmShouhouReconditionMapper.deleteFanxinInfo(snId);
        epmShouhouReconditionMapper.deleteFanxin(snId);


    }
}